﻿Imports MyDLL

Public Class Form5

    Dim Nav As New MyDLL.RecordNav

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim C As String = TextBox7.Text

        If CheckBox1.Checked Then
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Favourites>" & vbCr _
                                      & "<Type>Internet Favourites</Type>" & vbCr
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & _
            "<FlowDocument xmlns=" & C & "http://schemas.microsoft.com/winfx/2006/xaml/presentation" & C & ">" & vbCr
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Count As String = TextBox6.Text

        If CheckBox1.Checked Then
            Dim Counter As XElement = _
            <Counter>
                <Count><%= Count %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter.ToString & vbCr & "</Favourites>"
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & "</FlowDocument>"
        End If
    End Sub

    Private Sub WriteData()
        Dim ID As Integer = TextBox6.Text
        Dim Title As String = TextBox1.Text
        Dim URL As String = TextBox2.Text

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Dim Pagexml As XElement = _
            <Page>
                <Title id=<%= ID %>><%= Title %></Title>
                <URL id=<%= ID %>><%= URL %></URL>
            </Page>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Pagexml.ToString & vbCr

        End If

        If CheckBox2.Checked Then

            Dim Pagexaml As XElement = _
            <List FontFamily="Arial" FontSize="11pt">
                <ListItem><Paragraph>ID:<%= ID %></Paragraph></ListItem>
                <ListItem><Paragraph>Title:<%= Title %></Paragraph></ListItem>
                <ListItem><Paragraph>URL:<%= URL %></Paragraph></ListItem>
            </List>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Pagexaml.ToString & vbCr
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WriteData()
    End Sub

    Private Sub LoadData()

        Try
            Me.Text = "Internet Favourites Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text

            Dim Favourites As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Title As XElement = _
                <Root>
                    <%= From el In Favourites.<Page>.<Title> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox1.Text = Title

            Dim URL As XElement = _
        <Root>
            <%= From el In Favourites.<Page>.<URL> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox2.Text = URL

            Dim Count As XElement = _
       <Root>
           <%= From el In Favourites.<Counter>.<Count> _
               Select el %>
       </Root>

            Dim Counter As String = Count

            Label7.Text = "There are " & Counter & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")

        End Try

    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        LoadData()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Do Until TextBox6.Text = TextBox8.Text
                CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData()
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

        LoadData()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Form15.Show()
        Form15.ToolStripTextBox1.Text = TextBox2.Text
        Form15.Nav()
    End Sub
End Class